package com.cremamobile.filemanager.treeview;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class AnimUtils {
	int ANIMATION_DURATION=500;//in milisecond

	public void expand(final View v) {
		v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		final int targtetHeight = v.getMeasuredHeight();

		v.getLayoutParams().height = 0;
		v.setVisibility(View.VISIBLE);
		Animation a = new Animation() {
			@Override
			protected void applyTransformation(float interpolatedTime, Transformation t) {
			   v.getLayoutParams().height = interpolatedTime == 1
			           ? LayoutParams.WRAP_CONTENT
			           : (int)(targtetHeight * interpolatedTime);
			   v.requestLayout();
			}

			@Override
			public boolean willChangeBounds() {
			   return true;
			}
		};

		// 1dp/ms
		a.setDuration(ANIMATION_DURATION);

		// a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
		v.startAnimation(a);
	}

	public void collapse(final View v) {
		final int initialHeight = v.getMeasuredHeight();

		Animation a = new Animation() {
			@Override
			protected void applyTransformation(float interpolatedTime, Transformation t) {
			   if(interpolatedTime == 1){
			       v.setVisibility(View.GONE);
			   }else{
			       v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
			       v.requestLayout();
			   }
			}

			@Override
			public boolean willChangeBounds() {
			   return true;
			}
		};

		// 1dp/ms
		a.setDuration(ANIMATION_DURATION);
		// a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
		v.startAnimation(a);
	}
}